package ciyuan.Utils;

import ciyuan.Item.ItemRegisterManager;
import ciyuan.Item.TypeInterface.WeaponType.BowType;
import ciyuan.Item.TypeInterface.WeaponType.CrossBowType;
import ciyuan.MainClass;
import net.minecraft.item.Item;
import net.minecraft.item.ItemModelsProperties;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
public class PropertyRegistry {

    @SubscribeEvent
    public static void propertyOverrideRegistry(FMLClientSetupEvent event) {

        for (Object registryObject : ItemRegisterManager.ITEMS.getEntries()) {
            if (registryObject instanceof RegistryObject) {
                Object o = ((RegistryObject) registryObject).get();
                if (o instanceof BowType) {
                    Item item = (Item) o;
                    event.enqueueWork(() -> {
                        ItemModelsProperties.register(item, new ResourceLocation(MainClass.MODID, "pull"), (itemStack, clientWorld, livingEntity) -> {
                            if (livingEntity == null) {
                                return 0.0F;
                            } else {
                                return livingEntity.getUseItem() != itemStack ? 0.0F : (float) (itemStack.getUseDuration() - livingEntity.getUseItemRemainingTicks()) / 20.0F;
                            }
                        });
                    });
                    event.enqueueWork( ()-> {
                        ItemModelsProperties.register(item, new ResourceLocation(MainClass.MODID,"pulling"), (itemStack, clientWorld, livingEntity) -> {
                            return livingEntity != null && livingEntity.isUsingItem() && livingEntity.getUseItem() == itemStack ? 1.0F : 0.0F;
                        });
                    });
                } else if (o instanceof CrossBowType) {
                    Item item = (Item) o;
                    event.enqueueWork( ()-> {
                        ItemModelsProperties.register(item,new ResourceLocation(MainClass.MODID,"pull"), (p_239427_0_, p_239427_1_, p_239427_2_) -> {
                            if (p_239427_2_ == null) {
                                return 0.0F;
                            } else {
                                return CrossBowType.isCharged(p_239427_0_) ? 0.0F : (float)(p_239427_0_.getUseDuration() - p_239427_2_.getUseItemRemainingTicks()) / (float)CrossBowType.getChargeDuration(p_239427_0_);
                            }
                        });
                    });
                    event.enqueueWork( ()-> {
                        ItemModelsProperties.register(item,new ResourceLocation(MainClass.MODID,"pulling"), (p_239426_0_, p_239426_1_, p_239426_2_) -> {
                            return p_239426_2_ != null && p_239426_2_.isUsingItem() && p_239426_2_.getUseItem() == p_239426_0_ && !CrossBowType.isCharged(p_239426_0_) ? 1.0F : 0.0F;
                        });
                    });
                    event.enqueueWork( ()-> {
                        ItemModelsProperties.register(item,new ResourceLocation(MainClass.MODID,"charged"), (p_239425_0_, p_239425_1_, p_239425_2_) -> {
                            return p_239425_2_ != null && CrossBowType.isCharged(p_239425_0_) ? 1.0F : 0.0F;
                        });
                    });
                }
            }
        }

    }
}